//
//  SetRateMsgItemVM.swift
//  kxf
//
//  Created by Elavatine on 2024/11/6.
//


import Foundation
import UIKit

class SetRateMsgItemVM: UIView {
    
    let selfHeight = kFitWidth(58)

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .COLOR_LIGHT_GREY
        
        return vi
    }()
    lazy var leftTitleLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 16, weight: .medium)
        
        return lab
    }()
    lazy var feeTextField : UITextField = {
        let text = UITextField()
        text.textColor = .black
        text.font = .systemFont(ofSize: 16, weight: .medium)
        text.placeholder = "0.00"
        text.textAlignment = .right
        text.keyboardType = .decimalPad
        text.delegate = self
        
        return text
    }()
    lazy var feeLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 16, weight: .regular)
        lab.text = "%"
        
        return lab
    }()
    lazy var addLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 20, weight: .regular)
        lab.text = "+"
        
        return lab
    }()
    lazy var extraTextField : UITextField = {
        let text = UITextField()
        text.textColor = .black
        text.font = .systemFont(ofSize: 16, weight: .medium)
        text.placeholder = "0"
        text.textAlignment = .right
        text.keyboardType = .decimalPad
        text.delegate = self
        
        return text
    }()
    lazy var extraLab : UILabel = {
        let lab = UILabel()
        lab.textColor = .COLOR_TEXT_BLACK666
        lab.font = .systemFont(ofSize: 16, weight: .regular)
        lab.text = "元/笔"
        
        return lab
    }()
    lazy var feeAreaLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .THEME
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        
        return lab
    }()
    lazy var extraAreaLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .THEME
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        
        return lab
    }()
}

extension SetRateMsgItemVM{
    func refreshUI(dict:NSDictionary,isMerc:Bool) {
        leftTitleLabel.text = "\(dict["productName"]as? String ?? "")"
        feeTextField.text = "\(dict["channelRate"]as? Double ?? 0.00)"
        extraTextField.text = "\(dict["channelSettlement"]as? Double ?? 0.00)"
        feeAreaLabel.text = "\(dict["channelRateMin"]as? Double ?? 0.00) ~ \(dict["channelRateMax"]as? Double ?? 0.00)"
        extraAreaLabel.text = "\(dict["channelSettlementMin"]as? Double ?? 0.00) ~ \(dict["channelSettlementMax"]as? Double ?? 0.00)"
        
//        if isMerc{
//            refreshUIForMerc()
//        }
    }
    func refreshUIForMerc() {
        self.isUserInteractionEnabled = false
        feeAreaLabel.isHidden = true
        extraAreaLabel.isHidden = true
        
        addLab.snp.remakeConstraints { make in
            make.centerY.lessThanOrEqualToSuperview()
            make.height.equalTo(kFitWidth(26))
            make.left.equalTo(kFitWidth(250))
        }
        feeLab.snp.remakeConstraints { make in
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(kFitWidth(-148))
        }
        feeTextField.snp.remakeConstraints { make in
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(feeLab.snp.left).offset(kFitWidth(-10))
        }
        extraTextField.snp.remakeConstraints { make in
            make.right.equalTo(kFitWidth(-65))
            make.centerY.lessThanOrEqualToSuperview()
        }
        extraLab.snp.remakeConstraints { make in
//            make.right.equalTo(kFitWidth(-20))
            make.left.equalTo(extraTextField.snp.right).offset(kFitWidth(2))
            make.centerY.lessThanOrEqualToSuperview()
        }
    }
}

extension SetRateMsgItemVM{
    func initUI(){
        addSubview(lineView)
        addSubview(leftTitleLabel)
        addSubview(feeLab)
        addSubview(feeTextField)
        addSubview(addLab)
        addSubview(extraLab)
        addSubview(extraTextField)
        addSubview(feeAreaLabel)
        addSubview(extraAreaLabel)
        
        setConstrait()
    }
    func setConstrait() {
        lineView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(kFitWidth(0.4))
        }
        leftTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(16))
            make.centerY.lessThanOrEqualToSuperview()
        }
        addLab.snp.makeConstraints { make in
            make.top.equalTo(kFitWidth(6))
            make.height.equalTo(kFitWidth(26))
            make.left.equalTo(kFitWidth(250))
        }
        feeLab.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualTo(addLab)
            make.right.equalTo(kFitWidth(-148))
        }
        feeTextField.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualTo(feeLab)
            make.right.equalTo(feeLab.snp.left).offset(kFitWidth(-10))
        }
        extraTextField.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-65))
            make.centerY.lessThanOrEqualTo(addLab)
        }
        extraLab.snp.makeConstraints { make in
//            make.right.equalTo(kFitWidth(-25))
            make.left.equalTo(extraTextField.snp.right).offset(kFitWidth(2))
            make.centerY.lessThanOrEqualTo(addLab)
        }
        feeAreaLabel.snp.makeConstraints { make in
            make.right.equalTo(feeLab)
            make.top.equalTo(feeLab.snp.bottom).offset(kFitWidth(3))
        }
        extraAreaLabel.snp.makeConstraints { make in
            make.right.equalTo(extraLab)
            make.top.equalTo(feeAreaLabel)
        }
    }
}


extension SetRateMsgItemVM:UITextFieldDelegate{
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "" {
            return true
        }
        if string == "."{
            let str = textField.text ?? ""
            if str.contains("."){
                return false
            }
            if str.count == 0{
                return false
            }
        }
        if textField.text?.count ?? 0 > 4{
            return false
        }
        if let numArr = textField.text?.components(separatedBy: "."){
            if numArr.count >= 2{
                let decimal = numArr[1]as? String ?? ""
                if decimal.count >= 2{
                    return false
                }
            }
        }
        return true
    }
}
